package br.gen.seam.util.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import br.gen.seam.util.jdbc.exception.JDBCException;

/**
 * 
 * @author Diego Gomes Araujo - diegogomesaraujo@gmail.com
 * @version 1.0
 *
 */
public class AbstractConnection {

    private String driverClass;
    
    private String url;

    private String user;

    private String password;

    private Connection connection;

    private Statement statement;

    private ResultSet resultSet;

    private static AbstractConnection abstractConnection;

    private AbstractConnection() {
    }

    public static AbstractConnection getInstance() {
        if (abstractConnection == null) {
            abstractConnection = new AbstractConnection();
        }
        return abstractConnection;
    }

    public void openConnection() throws JDBCException {
        if (driverClass == null || driverClass.trim().length() == 0) {
            throw new JDBCException("Error: Driver JDBC not specified");
        }
        if (url == null || url.trim().length() == 0) {
            throw new JDBCException("Error: URL of connection JDBC not specified");
        }
        if (user == null) {
            throw new JDBCException("Error: User of database not specified");
        }
        try {
            Class.forName(driverClass);
            connection = DriverManager.getConnection(url, user, password);
            statement = connection.createStatement();
        } catch (ClassNotFoundException ex) {
            throw new JDBCException("Error when load class of driver JDBC (" + driverClass + ")", ex);
        } catch (SQLException ex) {
            throw new JDBCException("Error when connecting in database: " + ex.getMessage(), ex);
        }
    }

    public void closeConnection() throws JDBCException {
        try {
            if (resultSet != null) {
                resultSet.close();
            }
            if (statement != null) {
                statement.close();
            }
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException ex) {
            throw new JDBCException("Error when closing connection with database: " + ex.getMessage(), ex);
        }
    }

    public void execute(String sql) throws JDBCException {
        try {
            resultSet = statement.executeQuery(sql);
        } catch (SQLException ex) {
            throw new JDBCException("Error in your query: " + ex.getMessage(), ex);
        }
    }

    public ResultSet executeQuery(String sql) throws JDBCException {
        execute(sql);
        return resultSet;
    }

    public ResultSet executeSpecifiedQuery(String sql) throws JDBCException {
        try {
            ResultSet resultSet = statement.executeQuery(sql);
            return resultSet;
        } catch (SQLException ex) {
            throw new JDBCException("Error in your query: " + ex.getMessage(), ex);
        }
    }

    public Connection getConnection() {
        return connection;
    }

    public void setDriverClass(String driverClass) {
        this.driverClass = driverClass;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Statement getStatement() {
        return statement;
    }

    public ResultSet getResultSet() {
        return resultSet;
    }
}
